<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Sets as Set;
use app\api\model\kuyou\applet\Prefix as Prefixs;
/**
 * 设置接口
 */
class Sets extends Common
{
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['getset'];

    /**
     * 获取设置
     * @ApiMethod (POST)
     * @param string $name 获取设置名称
     */
    public function index(){
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $type = $this->request->post('type');
            $set_type = $this->request->post('set_type');
            if($set_type==1){
                $list = Prefixs::getPrefix($type);
            }else{
                $list = Set::getSet($name,$type);
            }
            if(empty($list)){
                $this->error('未查询到相关设置','',406);
            }else{
                $this->success('查询成功',$list,200);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新设置
     * @ApiMethod (POST)
     * @param array $set 设置数组
     */
    public function edit(){
        if ($this->request->isPost()) {
            $set = $this->request->post('set');
            $set = (array)json_decode(html_entity_decode($set), true);
            $set_type = $this->request->post('set_type');
            if($set_type==1){
                $ret = Prefixs::setPrefix($set);
            }else{
                $ret = Set::setSet($set);
            }
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
